package com.depin.pdaapp.activity;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.depin.pdaapp.R;
import com.depin.pdaapp.bean.UserBean;
import com.depin.pdaapp.constant.Constant;
import com.depin.pdaapp.http.Api;
import com.depin.pdaapp.presenter.LoginPresenter;
import com.depin.pdaapp.ui.PhoneEditText;
import com.jaydenxiao.common.base.BaseActivity;
import com.vondear.rxtool.RxRegTool;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

import static com.depin.pdaapp.http.ApiConstants.TYPE_HOST;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginPresenter.View {


    @BindView(R.id.et_account)
    PhoneEditText etAccount;
    @BindView(R.id.img_ver)
    ImageView imgVer;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    int type;
    String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA};

    public static void start(Activity activity, int type) {
        Intent intent = new Intent(activity, LoginActivity.class);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this);
        type = getIntent().getIntExtra("type", 0);

    }

    @Override
    public void initView() {
        if (!EasyPermissions.hasPermissions(LoginActivity.this, perms)) {
            EasyPermissions.requestPermissions(LoginActivity.this, "App正常运行需要存储权限、媒体权限", 1, perms);
        }
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phone = s.toString().replaceAll(" ", "");
                if (RxRegTool.isMobileExact(phone)) {
                    imgVer.setVisibility(View.VISIBLE);
                } else {
                    imgVer.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (etAccount.getText().toString().equals("") || etPassword.getText().equals("")) {
                    RxToast.showToast("手机号和密码不能为空");
                    return;
                }
                mPresenter.login(etAccount.getText().toString().replaceAll(" ", ""), etPassword.getText().toString(), type);
                break;

        }
    }

    @Override
    public void loginSuccess(UserBean userbean) {
        RxSPTool.putString(this, Constant.TOKEN, userbean.getToken());
        RxSPTool.putString(this, Constant.USERID, userbean.getAccount_id());
        startActivity(MainActivity.class);
        Api.clearAPi(TYPE_HOST);
    }
}